package com.hw.disruptor.eventTranslator;

import com.hw.disruptor.event.LongEvent;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;

/**
 * @program: disruptor-quickstart
 * @author: hewei
 * @date: 2023-07-06 17:09
 *
 * <p>
 *     Translator可以分离出来并且更加容易单元测试。Disruptor提供了不同的接口(EventTranslator, EventTranslatorOneArg, EventTranslatorTwoArg, 等等)去产生一个Translator对象。
 *     Translator中方法的参数是通过RingBuffer来传递的。
 * </p>
 **/
public class LongEventTranslator {

    private final RingBuffer<LongEvent> RING_BUFFER;

    //一个translator可以看做一个事件初始化器，publicEvent方法会调用它
    private final EventTranslatorOneArg<LongEvent, Long> TRANSLATOR ;

    public LongEventTranslator(RingBuffer<LongEvent> ringBuffer, EventTranslatorOneArg<LongEvent, Long> TRANSLATOR) {
        this.RING_BUFFER = ringBuffer;
        this.TRANSLATOR = TRANSLATOR;
    }

    public void publishData(Long data) {
        RING_BUFFER.publishEvent(TRANSLATOR, data);
    }

}
